package com.jboy.controller;

import com.jboy.entity.Person;

public class UserContextHolder {
    private static final ThreadLocal<Person> userThreadLocal = new ThreadLocal<>();
    
    public static void setUser(Person user) {
        userThreadLocal.set(user);
    }
    
    public static Person getUser() {
        return userThreadLocal.get();
    }
    
    public static void clear() {
        userThreadLocal.remove(); // 避免内存泄漏
    }

    public static void main(String[] args) {
        Person person = new Person();
        person.setName("jboy");
        person.setName("jboy1");
        UserContextHolder.setUser(person);
        System.out.println(UserContextHolder.getUser().getName());
        System.out.println(UserContextHolder.getUser().getName());
    }
}